/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Depart;
import model.Student;

public class StudentDao {
		
	public boolean addInfo(Student student){
		boolean flag;
		Connection con = DbConnection.getConnection();
		PreparedStatement ps;
		String sql = "";
//		ResultSet rs;
		try {
			sql = "insert into student values(?,?,?,?,?,?)"; //用问号代替

			ps = con.prepareStatement(sql);
			ps.setInt(1, student.getId());
			ps.setString(2, student.getName());
			ps.setString(3, student.getSex());
			ps.setString(4, student.getBorn_date());
			ps.setString(5, student.getDepart_id());
			ps.setString(6, student.getClass_name());
			ps.execute();
			System.out.println("添加了一条记录");
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag =  false;
		}finally{
			DbConnection.closeConnection(con);
		}
		return flag;
	}
	
	
	
	
	public ArrayList<Student> findAll(){
		ArrayList<Student> students = new ArrayList<Student>();
		Connection con = DbConnection.getConnection();
		Statement sta;
		ResultSet rs;
		try {
			sta = con.createStatement();
			rs = sta.executeQuery("SELECT * FROM student");  //从表depart中查询所有数据
			while(rs.next()){
				Student student = new Student();
				student.setId(rs.getInt(1));
				student.setName(rs.getString(2));
				student.setSex(rs.getString(3));
				student.setBorn_date(rs.getString(4));
				student.setDepart_id(rs.getString(5));
				student.setClass_name(rs.getString(6));
				
				System.out.println(student.getName());
				students.add(student);
			}
			rs.close();
			sta.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbConnection.closeConnection(con);
		}
		return students;
	}
	
	public Student findOne(int id){
		Student student = new Student();
		Connection con = DbConnection.getConnection();
		PreparedStatement ps;
		String sql = "";
		ResultSet rs;
		try {
			sql = "SELECT * FROM student where id=?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery(); 			 //从表depart中查询所有数据
			if(rs.next()){
				student.setId(rs.getInt("id"));  //注意数据类型要对应
				student.setName(rs.getString("Name"));
				student.setSex(rs.getString("Sex"));
				student.setBorn_date(rs.getString("birthday"));
				student.setDepart_id(rs.getString("Depart"));
				student.setClass_name(rs.getString("Class"));
				System.out.println("id:"+student.getId()+"  name:"+student.getName());
			}else{
				System.out.println("没有数据");
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbConnection.closeConnection(con);
		}
		return student;
	}
	
	public boolean deleteInfo(Student student){
		boolean flag;
		Connection con = DbConnection.getConnection();
		PreparedStatement ps;
		String sql = "";
		try {
			sql = "delete from student where  id=?"; //用问号代替

			ps = con.prepareStatement(sql);
			ps.setInt(1, student.getId());
			ps.execute();
			System.out.println("成功删除了一条记录");
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag =  false;
		}finally{
			DbConnection.closeConnection(con);
		}
		return flag;
	}
}
